﻿using Microsoft.Graphics.Canvas.Effects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;
using 纸片人.InfoDataFolder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{
    [PageType("gpu", "极坐标")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class CoorPlanePolarPage : Page
    {
        public CoorPlanePolarPage()
        {
            this.InitializeComponent();
            Init();
        }
        LayerBitmapData bitmapData = new LayerBitmapData();
        int model = 1;
        float innerRadius = 0.1f;
        async Task LoadShaderCode()
        {
            await bitmapData.LoadOneShadeCodeAsync(Shaders.ShaderType.PolarPlaneCoorr);
            await bitmapData.LoadTwoShadeCodeAsync(Shaders.ShaderType.CoorPlanePolar);
            await bitmapData.LoadThreeShadeCodeAsync(Shaders.ShaderType.SectorGraphics);
        }
        private void Init()
        {
            selectPicture.Click += async (s, e) => { await bitmapData.SelectOneBitmapAsync(); canvas.Invalidate(); };
            canvas.CreateResources += (s, e) =>
            {
                e.TrackAsyncAction(LoadShaderCode().AsAsyncAction());
            };
            canvas.Draw += (s, e) =>
            {
                Function.DrawGridGraphics(e.DrawingSession);

                if (bitmapData.oneBitmap != null && bitmapData.oneShaderCode != null)
                {
                    var element = (FrameworkElement)s;
                    float effectWidth = (float)element.ActualWidth;
                    float effectHeight = (float)element.ActualHeight * 0.7f;
                    float previewWidth = effectWidth;
                    float previewHeight = (float)element.ActualHeight * 0.3f;


                    var previewTran = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    previewTran.Source = bitmapData.oneBitmap;
                    e.DrawingSession.DrawImage(previewTran, 0, effectHeight);

                    PixelShaderEffect effect;
                    if (model < 3)
                    {
                        effect = new PixelShaderEffect(model == 1 ? bitmapData.oneShaderCode : bitmapData.twoShaderCode)
                        {
                            Source1 = bitmapData.oneBitmap,
                        };
                    }
                    else
                    {
                        effect = new PixelShaderEffect( bitmapData.threeShaderCode)
                        {
                            Source1 = bitmapData.oneBitmap,
                            Properties = {["innerCircleRadius"] = innerRadius},
                                
                            
                        };
                    }

                  



                    var effectTran = Function.CalcutateImageCenteredTransform(effectWidth, effectHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    effectTran.Source = effect;
                    e.DrawingSession.DrawImage(effectTran);
                }
            };


            polarPlanceCoorr.Checked += (s, e) =>
            {
                model = 1;
                canvas.Invalidate();

            };

            coorPlanePolar.Checked += (s, e) =>
            {
                model = 2;
                canvas.Invalidate();
            };

            sectorGraphics.Checked += (s, e) =>
            {
                model = 3;
                canvas.Invalidate();
            };

            innerCircleRadius.ValueChanged += (s, e) =>
            {
                innerRadius = (float)innerCircleRadius.Value;
                canvas.Invalidate();
            };


        }
    }
}
